package web.servlets;

import domain.Item;
import domain.Product;
import persistence.Impl.TIMEtil;
import service.CatelogService;
import service.DialogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @projectName: petStore
 * @package: web.servlets
 * @className: ViewItemServlet
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/11/1 10:39
 * @version: 1.0
 */
public class ViewItemServlet extends HttpServlet {
    private static final String VIEW_ITEM="/WEB-INF/jsp/catalog/Item.jsp";
    private String itemId;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        itemId=req.getParameter("itemId");

        CatelogService catelogService =new CatelogService();
        Item item=catelogService.getItem(itemId);
        Product product=catelogService.getProduct(item.getProductId());

        //diary 后期账号做好之后 选择判断存储账号还是IP
//        DialogService dialogService=new DialogService();
//        dialogService.insertNewDiary(req.getRemoteAddr().toString(), TIMEtil.getTime(),product.getCategoryId(),item.getProductId(),itemId,0);

        HttpSession httpSession=req.getSession();
        httpSession.setAttribute("product",product);
        httpSession.setAttribute("item",item);
        httpSession.setAttribute("symbol",0);

        req.getRequestDispatcher(VIEW_ITEM).forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
